#include <QMessageBox>
#include <QFileDialog>
#include <QMimeData>
#include <QPainter>
#include <QWidget>

#include "window/FileInformation.hpp"
#include "window/ImagesList.hpp"
#include "window/Window.hpp"
#include "window/Editor.hpp"
#include "tool/BezierCurve.hpp"

Window::Window(QStringList filepaths, QLocalServer* server) : QMainWindow(nullptr){
    this->server = server;

    // 菜单栏
    this->menuBar = new QMenuBar(this);

    this->fileMenu      = new QMenu("文件(&F)", this);
    this->editMenu      = new QMenu("编辑(&E)", this);
    this->selectionMenu = new QMenu("选择(&S)", this);
    this->viewMenu      = new QMenu("视图(&V)", this);
    this->helpMenu      = new QMenu("帮助(&H)", this);

    this->newAction          = new QAction("新建文件", this);
    this->openAction         = new QAction("打开文件", this);
    this->saveAction         = new QAction("保存文件", this);
    this->saveAllAction      = new QAction("保存全部", this);
    this->closeAction        = new QAction("关闭标签", this);
    this->informationAction  = new QAction("文件信息", this);
    this->imagesListAction   = new QAction("图像列表", this);
    this->exitAction         = new QAction("退出程序", this);

    this->previousPageAction = new QAction("上一页", this);
    this->nextPageAction     = new QAction("下一页", this);
    this->addNoteAction      = new QAction("添加注释", this);
    this->removeNoteAction   = new QAction("删除注释", this);
    // this->undoAction  = new QAction("撤销", this);
    // this->redoAction  = new QAction("重做", this);
    // this->cutAction   = new QAction("剪切", this);
    // this->copyAction  = new QAction("复制", this);
    // this->pasteAction = new QAction("粘贴", this);

    this->selectPreviousNoteAction          = new QAction("选中上一个注释", this);
    this->selectNextNoteAction              = new QAction("选中下一个注释", this);
    this->selectAndLocatePreviousNoteAction = new QAction("选中并定位上一个注释", this);
    this->selectAndLocateNextNoteAction     = new QAction("选中并定位下一个注释", this);
    this->locateSelectedNoteAction          = new QAction("定位选中的注释", this);

    this->zoomInAction       = new QAction("图像放大", this);
    this->zoomOutAction      = new QAction("图像缩小", this);
    this->viewResetAction    = new QAction("图像重置", this);
    this->topWindowAction    = new QAction("置顶窗口", this);

    this->checkUpdateAction = new QAction("检查更新", this);
    this->aboutAction       = new QAction("关于", this);

    this->tabWidget = new QTabWidget();

    // 汉化组徽标
    this->logo = new QImage(":/res/logo.png");

    // 设置菜单快捷键
    this->newAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_N));
    this->openAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_O));
    this->saveAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S));
    this->saveAllAction->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_S));
    this->closeAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_W));
    this->informationAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_I));
    this->imagesListAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_L));
    this->exitAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q));

    this->previousPageAction->setShortcut(QKeySequence(Qt::Key_A));
    this->nextPageAction->setShortcut(QKeySequence(Qt::Key_D));
    this->addNoteAction->setShortcut(QKeySequence(Qt::Key_N));
    this->removeNoteAction->setShortcut(QKeySequence(Qt::Key_Delete));

    this->selectPreviousNoteAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_A));
    this->selectNextNoteAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_D));
    this->selectAndLocatePreviousNoteAction->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT |Qt::Key_A));
    this->selectAndLocateNextNoteAction->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_D));
    this->locateSelectedNoteAction->setShortcut(QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_S));

    this->zoomInAction->setShortcut(QKeySequence(Qt::Key_Plus));
    this->zoomOutAction->setShortcut(QKeySequence(Qt::Key_Minus));
    this->viewResetAction->setShortcut(QKeySequence(Qt::Key_R));
    this->topWindowAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_T));

    // 连接菜单栏
    this->menuBar->addMenu(this->fileMenu);
    this->menuBar->addMenu(this->editMenu);
    // this->menuBar->addMenu(this->selectionMenu);
    this->menuBar->addMenu(this->viewMenu);
    this->menuBar->addMenu(this->helpMenu);

    this->fileMenu->addAction(this->newAction);
    this->fileMenu->addAction(this->openAction);
    this->fileMenu->addAction(this->saveAction);
    this->fileMenu->addAction(this->saveAllAction);
    this->fileMenu->addAction(this->closeAction);
    this->fileMenu->addSeparator();
    this->fileMenu->addAction(this->informationAction);
    this->fileMenu->addAction(this->imagesListAction);
    this->fileMenu->addSeparator();
    this->fileMenu->addAction(this->exitAction);

    this->editMenu->addAction(this->previousPageAction);
    this->editMenu->addAction(this->nextPageAction);
    this->editMenu->addSeparator();
    this->editMenu->addAction(this->addNoteAction);
    this->editMenu->addAction(this->removeNoteAction);
    // this->editMenu->addAction(this->undoAction);
    // this->editMenu->addAction(this->redoAction);
    // this->editMenu->addSeparator();
    // this->editMenu->addAction(this->cutAction);
    // this->editMenu->addAction(this->copyAction);
    // this->editMenu->addAction(this->pasteAction);

    // this->selectionMenu->addAction(this->selectPreviousNoteAction);
    // this->selectionMenu->addAction(this->selectNextNoteAction);
    // this->selectionMenu->addSeparator();
    // this->selectionMenu->addAction(this->selectAndLocatePreviousNoteAction);
    // this->selectionMenu->addAction(this->selectAndLocateNextNoteAction);
    // this->selectionMenu->addSeparator();
    // this->selectionMenu->addAction(this->locateSelectedNoteAction);

    // this->viewMenu->addAction(this->zoomInAction);
    // this->viewMenu->addAction(this->zoomOutAction);
    // this->viewMenu->addSeparator();
    // this->viewMenu->addAction(this->viewResetAction);
    // this->viewMenu->addSeparator();
    this->viewMenu->addAction(this->topWindowAction);

    // this->helpMenu->addAction(this->checkUpdateAction);
    // this->helpMenu->addSeparator();
    this->helpMenu->addAction(this->aboutAction);

    this->saveAction->setDisabled(true);
    this->saveAllAction->setDisabled(true);
    this->closeAction->setDisabled(true);
    this->informationAction->setDisabled(true);
    this->imagesListAction->setDisabled(true);

    this->previousPageAction->setDisabled(true);
    this->nextPageAction->setDisabled(true);
    this->addNoteAction->setDisabled(true);
    this->removeNoteAction->setDisabled(true);

    this->selectPreviousNoteAction->setDisabled(true);
    this->selectNextNoteAction->setDisabled(true);
    this->selectAndLocatePreviousNoteAction->setDisabled(true);
    this->selectAndLocateNextNoteAction->setDisabled(true);
    this->locateSelectedNoteAction->setDisabled(true);

    this->zoomInAction->setDisabled(true);
    this->zoomOutAction->setDisabled(true);
    this->viewResetAction->setDisabled(true);
    this->topWindowAction->setCheckable(true);

    this->tabWidget->setTabsClosable(true);
    this->tabWidget->setMovable(true);
    this->tabWidget->hide();

    // 信号连接
    QObject::connect(this->server, SIGNAL(newConnection()), this, SLOT(newConnection()));
    QObject::connect(this->tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(removeTab(int)));

    QObject::connect(this->newAction, SIGNAL(triggered()), this, SLOT(newFile()));
    QObject::connect(this->openAction, SIGNAL(triggered()), this, SLOT(openFile()));
    QObject::connect(this->saveAction, SIGNAL(triggered()), this, SLOT(saveFile()));
    QObject::connect(this->saveAllAction, SIGNAL(triggered()), this, SLOT(saveAllFiles()));
    QObject::connect(this->closeAction, SIGNAL(triggered()), this, SLOT(closeView()));
    QObject::connect(this->informationAction, SIGNAL(triggered()), this, SLOT(fileInformation()));
    QObject::connect(this->imagesListAction, SIGNAL(triggered()), this, SLOT(imagesList()));
    QObject::connect(this->exitAction, SIGNAL(triggered()), this, SLOT(close()));

    // QObject::connect(this->previousPageAction, SIGNAL(triggered()), this, SLOT(previousPage()));
    // QObject::connect(this->nextPageAction, SIGNAL(triggered()), this, SLOT(nextPage()));
    // QObject::connect(this->addNoteAction, SIGNAL(triggered()), this, SLOT(addNote()));
    // QObject::connect(this->removeNoteAction, SIGNAL(triggered()), this, SLOT(removeNote()));

    QObject::connect(this->topWindowAction, SIGNAL(triggered()), this, SLOT(topWindow()));

    QObject::connect(this->aboutAction, SIGNAL(triggered()), this, SLOT(about()));

    QObject::connect(this->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentChanged(int)));

    // 窗口参数
    this->setWindowTitle("苍眼汉化组");
    this->setMinimumSize(640, 480);
    this->setMenuBar(this->menuBar);
    this->setWindowIcon(QIcon(":/res/icon.png"));
    this->setCentralWidget(this->tabWidget);
    this->setAcceptDrops(true);
    this->resize(960, 640);

    // 判断是否打开文件
    if(filepaths.size() != 0){
        for(QString filepath: filepaths){
            CYFile* file = new CYFile(filepath);
            file->loadFile();

            Editor* editor = new Editor(file);
            this->tabWidget->addTab(editor, file->getFileName());

        }

        this->tabWidget->show();

    }

}

Window::~Window(){
    // 清除菜单栏
    delete this->newAction;
    delete this->openAction;
    delete this->saveAction;
    delete this->closeAction;
    delete this->informationAction;
    delete this->imagesListAction;
    delete this->exitAction;

    delete this->checkUpdateAction;
    delete this->aboutAction;

    delete this->fileMenu;
    delete this->editMenu;
    delete this->viewMenu;
    delete this->helpMenu;

    delete this->menuBar;

    // 清除图标
    delete this->logo;

}

void Window::updateAction(){
    if(this->tabWidget->count() != 0){
        Editor* editor = (Editor*)this->tabWidget->currentWidget();

        // 保存菜单
        if(editor->isSaved()){
            this->saveAction->setDisabled(true);

        }else{
            this->saveAction->setDisabled(false);

        }

        this->saveAllAction->setDisabled(true);

        for(int i = 0; i < this->tabWidget->count(); i++){
            Editor* widget = (Editor*)this->tabWidget->widget(i);

            if(!widget->isSaved()){
                this->saveAllAction->setDisabled(false);
                break;
                
            }

        }

        // 翻页菜单
        if(editor->getImageViewer()->getIndex() == 0){
            this->previousPageAction->setDisabled(true);

        }else{
            this->previousPageAction->setDisabled(false);

        }

        if(editor->getImageViewer()->getIndex() == editor->getImageViewer()->getSize() - 1){
            this->nextPageAction->setDisabled(true);

        }else{
            this->nextPageAction->setDisabled(false);

        }

        // 注释菜单
        this->addNoteAction->setDisabled(editor->isAddingNote());
        this->removeNoteAction->setDisabled(editor->getImageViewer()->getFocusIndex() == 0);

        // 选择菜单
        // if(editor->getImageViewer()->getSize() == 0){
        //     this->selectPreviousNoteAction->setDisabled(true);
        //     this->selectNextNoteAction->setDisabled(true);

        // }else{
        //     if(editor->getFile()->getImage(editor->getImageViewer()->getIndex()).getNotesCount() == 0){
        //         this->selectPreviousNoteAction->setDisabled(true);
        //         this->selectNextNoteAction->setDisabled(true);

        //     }else{
        //         this->selectPreviousNoteAction->setDisabled(true);
        //         this->selectNextNoteAction->setDisabled(true);

        //     }

        //     if(editor->getImageViewer()->getFocusIndex() == 0){
        //         this->selectPreviousNoteAction->setDisabled(true);

        //         if(editor->getFile()->getImage(editor->getImageViewer()->getIndex()).getNotesCount() == 1){
        //             this->selectNextNoteAction->setDisabled(true);

        //         }else{
        //             this->selectNextNoteAction->setDisabled(false);

        //         }

        //     }else{
        //         this->selectPreviousNoteAction->setDisabled(false);

        //         if(editor->getFile()->getImage(editor->getImageViewer()->getIndex()).getNotesCount() == 1){
        //             this->selectNextNoteAction->setDisabled(true);

        //         }else{
        //             this->selectNextNoteAction->setDisabled(false);

        //         }

        //     }

        // }

    }else{
        this->addNoteAction->setDisabled(true);
        this->removeNoteAction->setDisabled(true);

    }

}

void Window::paintEvent(QPaintEvent*){
    QPainter p = QPainter(this);

    // 组徽显示动画
    std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
    static long startTime = 0;

    p.setOpacity(0);

    long deltaTime = ms.count() - startTime - 500;

    if(startTime == 0){
        startTime = ms.count();
        this->update();
        
    }else{
        float process = deltaTime / 1000.0;

        if(process <= 1){
            p.setOpacity(process);
            this->update();

        }else{
            p.setOpacity(1);

        }

    }

    // 绘制组徽
    p.drawImage((this->width() - this->logo->width()) / 2, (this->height() - this->logo->height()) / 2, *this->logo);

}

void Window::mouseDoubleClickEvent(QMouseEvent*){
    if(this->tabWidget->count() == 0){
        QStringList filepaths = QFileDialog::getOpenFileNames(this, QStringLiteral("打开文件"), "./", QStringLiteral("翻译文件(*cy)"));

        if(filepaths.size() != 0){
            for(QString filepath: filepaths){
                CYFile* file = new CYFile(filepath);
                Byte result = file->loadFile();

                if(result == 0){
                    this->addTab(file);

                }else if(result == 1){
                    QMessageBox::critical(this, "错误", "程序无法读取该文件的内容");

                    delete file;

                }else if(result == 2){
                    QMessageBox::critical(this, "错误", "文件头部数据校验错误，该文件可能不是规范的 .cy 文件");

                    delete file;

                }else if(result == 3){
                    QMessageBox::critical(this, "错误", QString("程序不兼容该文件版本，建议更换更高版本的程序尝试打开"));

                }

            }

        }

    }

}

void Window::dragEnterEvent(QDragEnterEvent* event){
    for(int i = 0; i < event->mimeData()->urls().size(); i++){
        if(event->mimeData()->urls().at(i).fileName().right(3).compare(".cy") != 0){
            event->ignore();
            return;

        }

    }

    event->acceptProposedAction();

}

void Window::dropEvent(QDropEvent* event){
    for(QUrl url: event->mimeData()->urls()){
        CYFile* file = new CYFile(url.toLocalFile());
        Byte result = file->loadFile();

        if(result == 0){
            this->addTab(file);

        }else if(result == 1){
            QMessageBox::critical(this, "错误", "程序无法读取该文件的内容");

            delete file;

        }else if(result == 2){
            QMessageBox::critical(this, "错误", "文件头部数据校验错误，该文件可能不是规范的 .cy 文件");

            delete file;

        }else if(result == 3){
            QMessageBox::critical(this, "错误", QString("程序不兼容该文件版本，建议更换更高版本的程序尝试打开"));

        }

    }

}

void Window::closeEvent(QCloseEvent* event){
    for(int i = 0; i < this->tabWidget->count(); i++){
        Editor* editor = (Editor*)this->tabWidget->widget(i);

        if(!editor->isSaved()){
            QMessageBox::StandardButton result = QMessageBox::question(this, "保存全部", QString("是否在关闭程序前保存全部未保存的文件？"), QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes, QMessageBox::StandardButton::Yes);

            if(result == QMessageBox::Yes){
                this->saveAllFiles();
                event->accept();

            }else if(result == QMessageBox::No){
                event->accept();

            }else if(result == QMessageBox::Cancel){
                event->ignore();

            }

            return;

        }

    }

}

void Window::newFile(){
    NewFile* newInterface = new NewFile(this);

    newInterface->setWindowTitle("新建文件");
    newInterface->setMinimumSize(640, 480);
    newInterface->resize(640, 480);

    QObject::connect(newInterface, SIGNAL(addTab(CYFile*)), this, SLOT(addTab(CYFile*)));

    newInterface->exec();

}

void Window::openFile(){
    QStringList filepaths = QFileDialog::getOpenFileNames(this, QStringLiteral("打开文件"), "./", QStringLiteral("翻译文件(*cy)"));

    if(filepaths.size() != 0){
        for(QString filepath: filepaths){
            CYFile* file = new CYFile(filepath);
            Byte result = file->loadFile();

            if(result == 0){
                this->addTab(file);

            }else if(result == 1){
                QMessageBox::critical(this, "错误", "程序无法读取该文件的内容");

                delete file;

            }else if(result == 2){
                QMessageBox::critical(this, "错误", "文件头部数据校验错误，该文件可能不是规范的 .cy 文件");

                delete file;

            }else if(result == 3){
                QMessageBox::critical(this, "错误", QString("程序不兼容该文件版本，建议更换更高版本的程序尝试打开"));

            }

        }

    }

}

void Window::saveFile(){
    Editor* editor = (Editor*)this->tabWidget->currentWidget();
    editor->saveFile();

}

void Window::saveAllFiles(){
    for(int i = 0; i < this->tabWidget->count(); i++){
        Editor* editor = (Editor*)this->tabWidget->widget(i);

        if(!editor->isSaved()){
            editor->saveFile();

            this->tabWidget->setTabText(i, editor->getFileName());

        }

    }

}

void Window::closeView(){
    this->removeTab(this->tabWidget->currentIndex());

}

void Window::fileInformation(){
    FileInformation fileInformation(((Editor*)this->tabWidget->currentWidget())->getFile(), this);

    fileInformation.exec();

}

void Window::imagesList(){
    ImagesList* imagesList = new ImagesList(((Editor*)this->tabWidget->currentWidget())->getFile(), (Editor*)this->tabWidget->currentWidget(), this);

    imagesList->setWindowTitle("图像列表");
    imagesList->setMinimumSize(640, 480);
    imagesList->resize(640, 480);

    imagesList->exec();

}

// void Window::previousPage(){
//     Editor* editor = (Editor*)this->tabWidget->currentWidget();

//     editor->getImageViewer()->previous();

//     // 翻页菜单
//     if(editor->getImageViewer()->getIndex() == 0){
//         this->previousPageAction->setDisabled(true);

//     }else{
//         this->previousPageAction->setDisabled(false);

//     }

//     if(editor->getImageViewer()->getIndex() == editor->getImageViewer()->getSize() - 1){
//         this->nextPageAction->setDisabled(true);

//     }else{
//         this->nextPageAction->setDisabled(false);

//     }

// }

// void Window::nextPage(){
//     Editor* editor = (Editor*)this->tabWidget->currentWidget();
    
//     editor->getImageViewer()->next();

//     // 翻页菜单
//     if(editor->getImageViewer()->getIndex() == 0){
//         this->previousPageAction->setDisabled(true);

//     }else{
//         this->previousPageAction->setDisabled(false);

//     }

//     if(editor->getImageViewer()->getIndex() == editor->getImageViewer()->getSize() - 1){
//         this->nextPageAction->setDisabled(true);

//     }else{
//         this->nextPageAction->setDisabled(false);

//     }

// }

// void Window::addNote(){
//     qDebug("添加注释");

// }

// void Window::removeNote(){
//     qDebug("删除注释");

//     Editor* editor = (Editor*)this->tabWidget->currentWidget();
//     editor->getImageViewer()->tryRemove();
    
// }

void Window::topWindow(){
    if(this->topWindowAction->isChecked()){
        this->setWindowFlags(Qt::WindowStaysOnTopHint);
        this->show();

    }else{
        this->setWindowFlags(Qt::Window);
        this->show();

    }

}

void Window::about(){
    QMessageBox::about(this, "关于", "由谷林为苍眼汉化组便于开展工作而设计的小程序~\n如果有任何更新建议都可以向谷林提出哦！\n\n特别鸣谢：\n柴白 绘制汉化组图标和程序图标！\n\n版本：1.1.32");

}

void Window::fileSaved(){
    Editor* editor = (Editor*)this->tabWidget->currentWidget();
    this->tabWidget->setTabText(this->tabWidget->currentIndex(), editor->getFileName());

    this->saveAction->setDisabled(true);
    this->saveAllAction->setDisabled(true);

    for(int i = 0; i < this->tabWidget->count(); i++){
        Editor* widget = (Editor*)this->tabWidget->widget(i);

        if(!widget->isSaved()){
            this->saveAllAction->setDisabled(false);
            break;
            
        }

    }

}

void Window::fileChanged(){
    Editor* editor = (Editor*)this->tabWidget->currentWidget();
    this->tabWidget->setTabText(this->tabWidget->currentIndex(), QString(" • %1").arg(editor->getFileName()));

    this->saveAction->setDisabled(false);
    this->saveAllAction->setDisabled(false);

}

void Window::addTab(CYFile* file){
    for(int i = 0; i < this->tabWidget->count(); i++){
        if(((Editor*)this->tabWidget->widget(i))->getFilePath().compare(file->getFilePath()) == 0){
            delete file;
            return;

        }

    }

    Editor* editor = new Editor(file);
    this->tabWidget->addTab(editor, file->getFileName());

    this->tabWidget->show();

    // 启用菜单栏点击
    this->closeAction->setDisabled(false);
    this->informationAction->setDisabled(false);
    this->imagesListAction->setDisabled(false);

    this->addNoteAction->setDisabled(false);

    // 连接信号
    QObject::connect(file, SIGNAL(fileSaved()), this, SLOT(fileSaved()));
    QObject::connect(file, SIGNAL(fileChanged()), this, SLOT(fileChanged()));

}

void Window::removeTab(int index){
    Editor* editor = (Editor*)this->tabWidget->widget(index);

    if(!editor->isSaved()){
        QMessageBox::StandardButton result = QMessageBox::question(this, "保存", "是否先进行最后一次保存后再进行打开操作？", QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes, QMessageBox::StandardButton::Yes);

        if(result == QMessageBox::StandardButton::Yes){
            editor->saveFile();

        }else if(result == QMessageBox::StandardButton::Cancel){
            return;

        }

    }

    delete editor;

    if(this->tabWidget->count() == 0){
        this->tabWidget->hide();

        // 禁用菜单栏点击
        this->closeAction->setDisabled(true);
        this->saveAction->setDisabled(true);
        this->saveAllAction->setDisabled(true);
        this->informationAction->setDisabled(true);
        this->imagesListAction->setDisabled(true);

        this->previousPageAction->setDisabled(true);
        this->nextPageAction->setDisabled(true);

    }

}

void Window::newConnection(){
    QLocalSocket* socket = this->server->nextPendingConnection();

    if(socket->waitForReadyRead()){
        QDataStream stream(socket);

        int count;
        stream.readRawData((char*)&count, 4);

        for(int i = 0; i < count; i++){
            int size;
            stream.readRawData((char*)&size, 4);

            char filepath[size];
            stream.readRawData(filepath, size);

            CYFile* file = new CYFile(filepath);
            Byte result = file->loadFile();

            this->addTab(file);

        }

    }

}

void Window::currentChanged(int index){
    // 更新菜单状态
    this->updateAction();

    if(this->tabWidget->count() != 0){
        Editor* editor = (Editor*)this->tabWidget->currentWidget();

        // 断开信号，防止信号反复连接导致重复触发
        QObject::disconnect(editor, 0, this, 0);

        this->previousPageAction->disconnect();
        this->nextPageAction->disconnect();
        this->addNoteAction->disconnect();
        this->removeNoteAction->disconnect();

        // 连接信号
        QObject::connect(this->previousPageAction, SIGNAL(triggered()), editor, SLOT(previousPage()));
        QObject::connect(this->nextPageAction, SIGNAL(triggered()), editor, SLOT(nextPage()));
        QObject::connect(this->addNoteAction, SIGNAL(triggered()), editor, SLOT(addNote()));
        QObject::connect(this->removeNoteAction, SIGNAL(triggered()), editor, SLOT(removeNote()));

        QObject::connect(editor, SIGNAL(pageChanged()), this, SLOT(pageChanged()));
        QObject::connect(editor, SIGNAL(focusChanged()), this, SLOT(focusChanged()));
        QObject::connect(editor, SIGNAL(adding()), this, SLOT(adding()));
        QObject::connect(editor, SIGNAL(added()), this, SLOT(added()));

    }

}

void Window::pageChanged(){
    Editor* editor = (Editor*)this->tabWidget->currentWidget();

    // 翻页菜单
    if(editor->getImageViewer()->getIndex() == 0){
        this->previousPageAction->setDisabled(true);

    }else{
        this->previousPageAction->setDisabled(false);

    }

    if(editor->getImageViewer()->getIndex() == editor->getImageViewer()->getSize() - 1){
        this->nextPageAction->setDisabled(true);

    }else{
        this->nextPageAction->setDisabled(false);

    }

}

void Window::focusChanged(){
    Editor* editor = (Editor*)this->tabWidget->currentWidget();
    ImageViewer* viewer = editor->getImageViewer();

    if(viewer->getFocusIndex() == 0){
        this->removeNoteAction->setDisabled(true);

    }else{
        this->removeNoteAction->setDisabled(false);

    }

}

void Window::adding(){
    this->addNoteAction->setDisabled(true);

}

void Window::added(){
    this->addNoteAction->setDisabled(false);

}
